import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_wan_android/bean/article_list_bean.dart';
import 'package:flutter_wan_android/blocs/article_bloc.dart';
import 'package:flutter_wan_android/blocs/base/bloc_provider.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/widgets/app_bar_widget.dart';
import 'package:flutter_wan_android/widgets/empty_widget.dart';
import 'package:flutter_wan_android/widgets/home_widgets.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class ArticlePage extends StatefulWidget {
  final String title;
  final int chapterId;

  const ArticlePage({Key key, this.title, this.chapterId}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _ArticleState(title,chapterId);
  }
}

class _ArticleState extends State {
  ArticleBloc _bloc;
  int _pageIndex = 1;
  final String title;
  final int chapterId;

  _ArticleState( this.title, this.chapterId);

  @override
  void initState() {
    super.initState();
    _bloc = BlocProvider.of<ArticleBloc>(context);
    Future.delayed(
        Duration(seconds: 2), () {
          _initData();

        });
  }

  void _initData() {
    if(title==null){
      _bloc.getArticleList(_pageIndex);
    }else{
     _bloc.getArticleList(_pageIndex,id: chapterId);
    }
  }

  @override
  void dispose() {
    super.dispose();
    _bloc.allData.clear();
    _bloc.dispose();
  }

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar.buildAppBar(context, title==null?"文章":title),
      body: StreamBuilder<List<ArticleItem>>(
          stream: _bloc.stream,
          builder: (context, AsyncSnapshot<List<ArticleItem>> snapshot) {
            if (_pageIndex == 1) {
              _refreshController.refreshCompleted();
            } else {
              _refreshController.loadComplete();
            }

            if(!snapshot.hasData){
              return LoadingView();
            }

            if (_bloc.allData.length == 0 && _pageIndex == 1) {
              return ErrorView(error: '暂无数据',onTap: (){
                _refresh();
              },);
            }

            return buildSmartRefresher();
          }),
    );
  }

  SmartRefresher buildSmartRefresher() {
    return SmartRefresher(
      child: ListView.separated(
          itemBuilder: (context, index) {
            return ArticleItemView(_bloc.allData.elementAt(index));
          },
          separatorBuilder: (context, index) {
            return Container(
              height: 0.5,
              color: Colors.transparent,
            );
          },
          itemCount: _bloc.allData.length),
      controller: _refreshController,
      enablePullUp: true,
      footer: ClassicFooter(
        loadStyle: LoadStyle.ShowAlways,
        completeDuration: Duration(milliseconds: 500),
      ),
      header: WaterDropMaterialHeader(
        backgroundColor: XColors.refreshStyleColor,
      ),
      onRefresh: () async {
        _refresh();
      },
      onLoading: () async {
        _loading();
      },
    );
  }

  void _loading() {
    ++_pageIndex;
    _initData();
  }

  void _refresh() {
    _pageIndex = 1;
    _initData();
  }
}
